package com.uwo.house.controller;

import com.uwo.house.beans.response.StaffResp;
import com.uwo.house.beans.response.CityResp;
import com.uwo.house.beans.response.DictionaryResp;
import com.uwo.house.beans.response.ProvinceResp;
import com.uwo.house.service.IDictionaryService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 数据字典Controller
 * Created by cyn1c on 2017/7/27.
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {
    @Autowired
    public IDictionaryService dictionaryService;

    @RequestMapping("/decoration")
    @ResponseBody
    public Map getDecorationStyleList() {
        List<DictionaryResp> list = getDictionaryList(this.DECORATION);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 获取材料类别列表
     *
     * @return
     */
    @RequestMapping("/material/class")
    @ResponseBody
    public Map getMaterialClassList() {
        List<DictionaryResp> list = getDictionaryList(this.MATERIALCLASS);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    public List<DictionaryResp> getDictionaryList(String classId) {
        List<DictionaryResp> list = this.dictionaryService.getDecorationStyleList(classId);
        return list;
    }

    public List<StaffResp> getStaffList(int roleId) {
        List<StaffResp> list = this.dictionaryService.getStaffList(roleId);
        return list;
    }

    @RequestMapping(value = "/province", method = RequestMethod.GET)
    @ResponseBody
    public Map getProvince() {
        List<ProvinceResp> list = this.dictionaryService.getProvince();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    @RequestMapping(value = "/city", method = RequestMethod.GET)
    @ResponseBody
    public Map getCityByParentId(@RequestParam(value = "id", required = false) int id) {
        List<CityResp> list = this.dictionaryService.getCityByParentId(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    @RequestMapping(value = "/province/open", method = RequestMethod.GET)
    @ResponseBody
    public Map getOpenProvince() {
        List<ProvinceResp> list = this.dictionaryService.getOpenProvince();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    @RequestMapping(value = "/city/open", method = RequestMethod.GET)
    @ResponseBody
    public Map getOpenCityByParentId(@RequestParam(value = "id", required = false) int id) {
        List<CityResp> list = this.dictionaryService.getOpenCityByParentId(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 下拉列表获取设计师列表
     *
     * @return
     */
    @RequestMapping(value = "/designer", method = RequestMethod.GET)
    @ResponseBody
    public Map getDesigners() {
        List<StaffResp> list = getStaffList(this.DESIGNER_NUMBER);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }


    /**
     * 获取监理列表
     *
     * @return
     */
    @RequestMapping(value = "/supervisor", method = RequestMethod.GET)
    @ResponseBody
    public Map getSupervisors() {
        List<StaffResp> list = getStaffList(this.SUPERVISOR_NUMBER);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 城市开通
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/open/city", method = RequestMethod.PUT)
    @ResponseBody
    public Map openCity(@RequestParam(value = "id", required = false) Integer id) {
        CityResp cityResp = dictionaryService.getCityInfoById(id);
        List<CityResp> list = this.dictionaryService.getOpenCityByParentId(cityResp.getProvinceId());//获取已开通城市省是否有已开通城市
        //如果未开通省/则先开通省
        if (list.isEmpty()) {
            dictionaryService.openProvince(cityResp.getProvinceId());//开通省
            dictionaryService.openCity(id);//开通市
        } else {
            dictionaryService.openCity(id);
        }
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "开通成功");
    }

    /**
     * 城市关闭
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/close/city", method = RequestMethod.PUT)
    @ResponseBody
    public Map closeCity(@RequestParam(value = "id", required = false) Integer id) {
        CityResp cityResp = dictionaryService.getCityInfoById(id);
        dictionaryService.closeCity(id);//关闭市
        List<CityResp> list = this.dictionaryService.getOpenCityByParentId(cityResp.getProvinceId());//获取已开通城市省是否有已开通城市
        //如果没有其他开通市  则关闭省
        if (list.isEmpty()) {
            dictionaryService.closeProvince(cityResp.getProvinceId());//关闭省
        }
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "关闭成功");
    }
}
